package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.DictConstants;
import com.bsg.upm.constant.DictTypeConstants;
import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.ServEntity;
import com.bsg.upm.entity.SubServEntity;

/**
 * 数据库业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class DatabaseCheck extends BaseCheck {

	/**
	 * 数据库新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 数据库编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdate(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkUpdateNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkUpdateLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 数据库编辑时检查
	 * 
	 * @param serv
	 *            服务对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(ServEntity serv) {
		CheckResult chkRS = new CheckResult();

		// 服务存在性检查
		if (serv == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "服务"));
			return chkRS;
		}

		List<SubServEntity> subServs = serv.getSubServs();
		boolean canSave = false;
		for (SubServEntity subServ : subServs) {
			if (DictConstants.DATABASE_TYPE_RDS.equals(subServ.getSubOrder().getDefinitionSubServ().getType())) {
				canSave = true;
				break;
			}
		}
		if (!canSave) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("该类型服务无数据库");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		String servId = (String) paramMap.get("servId");
		if (StringUtils.isBlank(servId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "服务编码"));
			return chkRS;
		}

		// 数据库名非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "数据库名"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "数据库名", 32));
			return chkRS;
		}

		// 字符集非空检查
		String characterSet = (String) paramMap.get("characterSet");
		if (StringUtils.isBlank(characterSet)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "字符集"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 字符集存在性检查
		String characterSet = (String) paramMap.get("characterSet");
		if (dictTypeCache.getDictFromCache(DictTypeConstants.CHARACTER_SET, characterSet) == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "字符集"));
			return chkRS;
		}

		String servId = (String) paramMap.get("servId");
		ServEntity serv = servDao.getBase(servId);
		if (serv == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "服务"));
			return chkRS;
		}
		
		List<SubServEntity> subServs = serv.getSubServs();
		boolean canSave = false;
		for (SubServEntity subServ : subServs) {
			if (DictConstants.DATABASE_TYPE_RDS.equals(subServ.getSubOrder().getDefinitionSubServ().getType())) {
				canSave = true;
				break;
			}
		}
		if (!canSave) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("该类型服务无数据库");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 编辑时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		String servId = (String) paramMap.get("servId");
		if (StringUtils.isBlank(servId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "服务编码"));
			return chkRS;
		}

		// 数据库名非空检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "数据库名"));
			return chkRS;
		}

		// 字符集非空检查
		String characterSet = (String) paramMap.get("characterSet");
		if (StringUtils.isBlank(characterSet)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "字符集"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 编辑时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 字符集存在性检查
		String characterSet = (String) paramMap.get("characterSet");
		if (dictTypeCache.getDictFromCache(DictTypeConstants.CHARACTER_SET, characterSet) == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "字符集"));
			return chkRS;
		}

		String servId = (String) paramMap.get("servId");
		ServEntity serv = servDao.getBase(servId);
		if (serv == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "服务"));
			return chkRS;
		}

		List<SubServEntity> subServs = serv.getSubServs();
		boolean canSave = false;
		for (SubServEntity subServ : subServs) {
			if (DictConstants.DATABASE_TYPE_RDS.equals(subServ.getSubOrder().getDefinitionSubServ().getType())) {
				canSave = true;
				break;
			}
		}
		if (!canSave) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("该类型服务无数据库");
			return chkRS;
		}
		return chkRS;
	}
}
